/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.jewelrystyle;

import com.kmcsoft.core.service.GenericService;
import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.model.master.JewelryStyle;
import com.kmcsoft.utils.i18n;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Listitem;

/**
 *
 * @author kmcsoft02
 */
public class JewelryStyleComposer extends GenericController<JewelryStyle, GenericService> {
    public void onCreate() {
        onClick$new(null);
    }

    @Override
    protected void doAfterNew() {
        super.doAfterNew();
    }


    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        if (isExistCode(selected)) {
            throw new WrongValueException(i18n.l("shop.jewelryStyle.error.duplicate.code"));
        }
        if (isExistName(selected)) {
            throw new WrongValueException(i18n.l("shop.jewelryStyle.error.duplicate.name"));
        }
    }

    private boolean isExistCode(JewelryStyle jewelryStyle) {
        JewelryStyle style = service.getObject(JewelryStyle.class, "code.toLowerCase()==pCode", "String pCode", jewelryStyle.getCode().toLowerCase());
        if (style == null) {
            return false;
        }
        if(style.getId()==jewelryStyle.getId()){
            return false;
        }
        return true;
    }
    private boolean isExistName(JewelryStyle jewelryStyle) {
        JewelryStyle style = service.getObject(JewelryStyle.class, "name.toLowerCase()==pName", "String pName", jewelryStyle.getName().toLowerCase());
        if (style == null) {
            return false;
        }
        if(style.getId()==jewelryStyle.getId()){
            return false;
        }
        return true;
    }

    @Override
    protected void doAfterDelete() {
        super.doAfterDelete();
        if(leftList.getListModel().getSize()>0){
            setSelected((JewelryStyle)(((Listitem)leftList.getChildren().get(0)).getValue()));
        }else{
            onClick$new(null);
        }
        
    }
}
